package com.meelive.ingkee.push.platform.hms

import android.os.Bundle
import android.util.Log
import com.huawei.hms.push.HmsMessageService
import com.huawei.hms.push.RemoteMessage
import com.meelive.ingkee.push.InkePushHelper
import com.meelive.ingkee.push.model.Constance
import com.meelive.ingkee.push.model.InkePushType
import java.lang.Exception

/**
 * Create by: fanfeiyue
 * on: 2019-08-25
 * desc: 透传消息在这里进行回调，通知栏消息因onEvent方法会逐渐废弃，所以采用
 * activity进行通知栏点击回调的接收
 */
class InkeHmsMessageService : HmsMessageService() {

    override fun onMessageReceived(message: RemoteMessage) {
        // 获取消息内容
        Log.i(
            Constance.TAG,
            """getData: ${message.data}        
            getFrom: ${message.from}        
            getTo: ${message.to}        
            getMessageId: ${message.messageId}
            getSentTime: ${message.sentTime}           
            getDataMap: ${message.dataOfMap}
            getMessageType: ${message.messageType}   
            getTtl: ${message.ttl}        
            getToken: ${message.token}""".trimIndent()
        )
        InkePushHelper.getInstance().onPassThroughPush(applicationContext, message.data, InkePushType.HMSPUSH)
    }

    override fun onTokenError(e: Exception?) {
        InkePushHelper.getInstance().onErrorResponse(InkePushType.HMSPUSH, Constance.PUSH_UNKNOW, e?.message)
    }

    override fun onTokenError(e: Exception?, p1: Bundle?) {
        InkePushHelper.getInstance().onErrorResponse(InkePushType.HMSPUSH, Constance.PUSH_UNKNOW, e?.message)
    }

    override fun onNewToken(token: String?) {
        if (token.isNullOrBlank()) return
        InkePushHelper.getInstance().onReceiveToken(InkePushType.HMSPUSH, token)
    }

    override fun onNewToken(token: String?, bundle: Bundle) {
        if (token.isNullOrBlank()) return
        InkePushHelper.getInstance().onReceiveToken(InkePushType.HMSPUSH, token)
    }
}